﻿using System;

namespace BizElements.Web
{
    internal class MultiChoiceCommand : JsCommand
    {
        public const string DefaultDelimiter = ";";

        #region Constructors.

        protected MultiChoiceCommand()
            : base()
        {
            this.MaxParameterSetSize = int.MaxValue;
        }

        public MultiChoiceCommand(string key, string text, string callFormat, string[] requiredParameters)
            : base(key, text, callFormat, requiredParameters)
        {
            this.MaxParameterSetSize = int.MaxValue;
        }

        #endregion

        #region Properties.

        string delimiter = DefaultDelimiter;

        public string Delimiter
        {
            get { return this.delimiter; }
            set { this.delimiter = value; }
        }

        #endregion

        #region GenerateClientScript.

        protected override string GenerateClientScript(string functionCallFormat, string[][] scriptParams)
        {
            int maxFieldCount = 0;
            foreach (string[] record in scriptParams)
            {
                if (record.Length > maxFieldCount)
                    maxFieldCount = record.Length;
            }

            string[] concatenatedParams = new string[maxFieldCount];
            int recordCount = scriptParams.Length;
            for (int idxRecord = 0; idxRecord < recordCount; idxRecord++)
            {
                bool isLastRecord = (idxRecord == recordCount - 1);
                string[] record = scriptParams[idxRecord];
                for (int idxField = 0; idxField < concatenatedParams.Length; idxField++)
                {
                    string field = record[idxField];
                    concatenatedParams[idxField] += field;
                    if (!isLastRecord)
                        concatenatedParams[idxField] += this.Delimiter;
                }
            }

            string script = string.Format(functionCallFormat, concatenatedParams);
            return script;
        }

        #endregion

        #region Clone, CopyFrom.

        /// <summary>
        /// Creates a new object that is a copy of the current instance. 
        /// </summary>
        /// <remarks>Must override in derived classes.</remarks>
        public override object Clone()
        {
            if (GetType() != typeof(MultiChoiceCommand))
                throw new InvalidOperationException("Clone method must be overriden in derived classes. Do not use \"base.Clone()\" in derived class.");

            MultiChoiceCommand clone = new MultiChoiceCommand();
            clone.CopyFrom(this);
            return clone;
        }

        /// <summary>
        /// Copies information from an <b>JsCommand</b> object. 
        /// </summary>
        /// <param name="source">The <b>JsCommand</b> object to base the new object on.</param>
        /// <remarks>This method copies information to an JsCommand object contained in the specified 
        /// JsCommand object. Override in derived classes if new class fields/members are introduced.</remarks>
        protected virtual void CopyFrom(MultiChoiceCommand source)
        {
            base.CopyFrom(source);
            this.CallFormat = source.CallFormat;
            this.Delimiter = source.Delimiter;
        }

        #endregion
    }
}
